package common.tool.box.system.domain.vo;

import common.tool.box.system.domain.SysUserFile;
import lombok.Data;

import java.util.List;

@Data
public class UserFileStorageVo {
    /**
     * 文件集合
     */
    private List<SysUserFile> sysUserFileList;

    /**
     * 文件总数
     */
    private Integer fileCount;

    /**
     * 文件总大小 MB
     */
    private Double fileTotalSizeMB;

    /**
     * 文件总大小 KB
     */
    private Double fileTotalSizeKB;

    /**
     * 文件分配大小 MB
     */
    private Double fileAllotSizeMB = 10.0;

    /**
     * 文件分配大小 KB
     */
    private Double fileAllotSizeKB = 10240.0;

    /**
     * 文件空闲大小 MB
     */
    private Double fileFreeSizeMB;

    /**
     * 文件空闲大小 KB
     */
    private Double fileFreeSizeKB;

    /**
     * 文件使用率
     */
    private Double fileUseRate;

    /**
     * 文件剩余率
     */
    private Double fileFreeRate;

    /**
     * 是否超出限制
     */
    private Boolean isOverLimit = false;

    /**
     * 文件限制数量
     */
    private Integer fileLimitCount;

    public void generator() {
        // 判断文件是否为空
        this.fileCount = sysUserFileList.size();
        if (sysUserFileList.size() == 0) {
            return;
        }
        this.fileTotalSizeMB = sysUserFileList.stream().mapToDouble(SysUserFile::getFileSize).sum() / 1024;
        this.fileTotalSizeKB = sysUserFileList.stream().mapToDouble(SysUserFile::getFileSize).sum();
        this.fileFreeSizeMB = fileAllotSizeMB - fileTotalSizeMB;
        this.fileFreeSizeKB = fileAllotSizeKB - fileTotalSizeKB;
        this.fileUseRate = fileTotalSizeMB / fileAllotSizeMB;
        this.fileFreeRate = fileFreeSizeMB / fileAllotSizeMB;
    }

    public void analyseUserType(int normalMaxCount, int silverMaxCount, int goldMaxCount) {
        // TODO 预留会员等级
        switch ("normal") {
            case "normal":
                this.isOverLimit = this.fileCount >= normalMaxCount;
                this.fileLimitCount = normalMaxCount;
                break;
            case "silver":
                this.isOverLimit = this.fileCount >= silverMaxCount;
                this.fileLimitCount = silverMaxCount;
                break;
            case "gold":
                this.isOverLimit = this.fileCount >= goldMaxCount;
                this.fileLimitCount = goldMaxCount;
                break;
            default:
                this.isOverLimit = false;
                this.fileLimitCount = normalMaxCount;
                break;
        }
    }

    public UserFileStorageVo(List<SysUserFile> sysUserFileList) {
        this.sysUserFileList = sysUserFileList;
    }
}
